[Refactor] Generalize magi_compile for non-module callables and simplify internals#17
Open
cennn wants to merge 1 commit intoSandAI-org:mainfrom
Open
Conversation
- Unify api.py dispatch: consolidate method_name / owner_cls / owner_obj inference, remove __call__ fallback, simplify entry kind identification - Remove unnecessary patch.object during first JIT compilation - Remove redundant _state_dispatches_via_method and dispatch_via_method - Remove redundant callable check in MagiCompileState.__init__ - Flatten dispatch_to_compiled_fwd: inline 4 sub-context-managers into one - Remove owner traversal in _mark_static_shapes (Dynamo handles self) - Simplify _lazy_init_magi_state: single obj param, unified model_tag - Rename internal state attrs: _target_callable -> original_entry, _compiled_callable -> compiled_entry - Lighten timing test model: dim=128 x 4 layers (was 256 x 8) - Use cuda_benchmark in api_tests for stable timing - Add non-module class/instance/method perf tests across all suites
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🗂️ PR Category
📝 Description
Non-module callable support
magi_compilenow works uniformly on any callable class/instance, not justnn.Module:__init__(same mechanism for Module and non-module).__self__.method_nameparameter allows explicit method selection (defaults toforward).AOT instance compilation fix
Previously, AOT mode for instance-level compilation (
magi_compile(model)) had issues withdispatch_to_compiled_fwd— the old code special-casednn.Modulewithisinstancechecks and_magi_original_forwardhacks. This is replaced by a clean two-category dispatch:target_method_nameis set): swaptarget_function.__code__and bind to instance.inspect.unwrap(obj).__code__directly.Both JIT and AOT use the same
dispatch_to_compiled_fwdwith a singledispatch_via_methodflag — no moreisinstance(obj, nn.Module)branches.Internal simplification
patch.objectduring first JIT compilation (unnecessary — Dynamo tracescompiled_entrybytecode directly, notinstance.__dict__)._state_dispatches_via_method,_magi_compile_instance(merged into_magi_compile_bound_method).dispatch_to_compiled_fwd: 4 sub-context-managers inlined into one.ownertraversal in_mark_static_shapes(Dynamo handlesself)._target_callable→original_entry,_compiled_callable→compiled_entry,compiled_code→jit_compiled_code,original_code_object→original_code_for_hook.get_attr_name_for_*helpers.Tests
cuda_benchmarkin API timing tests for stability.